﻿using System;
using System.Windows.Forms;
using FoundationClass;
using FoundationClass.Entity;
using Telerik.WinControls;

namespace P2MARY_INTERNET
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            try
            {
                if (!InitializeLogin())
                {
                    CloseNow = true;
                    return;
                }

                InitializeComponent();
                ((tabOperation.GetChildAt(0).GetChildAt(0))).Visibility = ElementVisibility.Collapsed;
                SetDisplay();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(@"MainForm :" + ex.Message);
            }
        }

        public User UserLogin { get; set; }
        public bool CloseNow { get; set; }

        private int _screenType = 2;

        private bool InitializeLogin()
        {
            var loginForm = new LoginForm {StartPosition = FormStartPosition.CenterScreen};
            if (loginForm.ShowDialog() ==DialogResult.OK )
            {
                UserLogin = loginForm.UserLogin;
                if (UserLogin == null)
                    return false;

                if (loginForm.UserLogin.Position == 0)
                    _screenType = 1;
                else
                    _screenType = 2;
                return true;
            }
            return false;
        }

        private void radImgEmployee_Click(object sender, EventArgs e)
        {
            tabOperation.SelectedPage = tabEmployee;
        }

        private void radImgPromotion_Click(object sender, EventArgs e)
        {
            tabOperation.SelectedPage = tabPromotion;
        }

        private void radImgComputer_Click(object sender, EventArgs e)
        {
            tabOperation.SelectedPage = tabComputer;
        }

        private void radImgOverall_Click(object sender, EventArgs e)
        {
            tabOperation.SelectedPage = tabOverall;
        }

        private void radImgPayment_Click(object sender, EventArgs e)
        {
            tabOperation.SelectedPage = tabPayment;
        }

        private void radImageButtonElement1_Click(object sender, EventArgs e)
        {
            tabOperation.SelectedPage = tabPaymentHistory;
            paymentHistoryForm1.Setup();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }

        private void radMenuItem2_Click(object sender, EventArgs e)
        {
            SecurityHandler.LogOut();
            Login();
        }


        private void Login()
        {
            try
            {
                var loginForm = new LoginForm { StartPosition = FormStartPosition.CenterScreen };
                if (loginForm.ShowDialog() == DialogResult.OK)
                {
                    if (loginForm.UserLogin.Position == 0)
                        _screenType = 1;
                    else
                        _screenType = 2;
                }
                SetDisplay();
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"Login : " + ex.Message);
            }
        }

        private void SetDisplay()
        {
            if (_screenType == 1)
            {
                radRibbonBar1.CommandTabs[0].Visibility = ElementVisibility.Visible;
                ribbonTab1.IsSelected = true;
                tabOperation.SelectedPage = tabEmployee;
            }
            else
            {
                radRibbonBar1.CommandTabs[0].Visibility = ElementVisibility.Collapsed;
                ribbonTab2.IsSelected = true;
                tabOperation.SelectedPage = tabOverall;
            }
            overAllForm1.UserLogin = UserLogin;
            paymentForm1.UserLogin = UserLogin;
        }

        private void radRibbonBar1_CommandTabSelected(object sender, Telerik.WinControls.UI.CommandTabEventArgs args)
        {
            if (tabOperation.SelectedPage == null) return;

            if (radRibbonBar1.SelectedCommandTab == ribbonTab2)
            {
                tabOperation.SelectedPage = tabOverall;
            }
            else
            {
                tabOperation.SelectedPage = tabEmployee;
            }
        }

        private void radRibbonBar1_Click(object sender, EventArgs e)
        {
            if (tabOperation.SelectedPage == null) return;

            if (radRibbonBar1.SelectedCommandTab == ribbonTab2)
            {
                tabOperation.SelectedPage = tabOverall;
            }
            else
            {
                tabOperation.SelectedPage = tabEmployee;
            }
        }
    }
}
